package com.qn.visitor.analyze.ipaddressr;

import com.qn.visitor.analyze.ipaddressr.strategy.cz88.Cz88Analyze;
import com.qn.visitor.analyze.ipaddressr.strategy.Ip2regionAnalyze;
import com.qn.visitor.analyze.ipaddressr.strategy.IpAddressrStrategy;
import com.qn.visitor.analyze.ipaddressr.strategy.ipwhois.IpwhoisAnalyze;
import com.qn.visitor.model.UserVisitorRequest;
import com.qn.visitor.model.bo.analyze.IpAddressModel;
import com.qn.visitor.resources.config.IpAddressrTypeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
/**
 * qingniu-cloud user source analysis
 *
 * @Author: TXG
 * @Date: 2024/6/11
 * @Version V0.0.2
 */
@Component
public class IpAddressrContext {
    @Autowired
    IpAddressrProperties properties;
    @Autowired
    ApplicationContext applicationContext;
    public IpAddressModel analyze(UserVisitorRequest ubrBo) throws Exception {
        if (properties.getIpAddressrType() == null) {
            return null;
        }
        IpAddressrStrategy strategy;
        if (properties.getIpAddressrType().equals(IpAddressrTypeUtils.IP2REGION)) {
            strategy = applicationContext.getBean(Ip2regionAnalyze.class);
        } else if (properties.getIpAddressrType().equals(IpAddressrTypeUtils.CZ88)) {
            strategy = applicationContext.getBean(Cz88Analyze.class);
        } else if (properties.getIpAddressrType().equals(IpAddressrTypeUtils.IPWHOIS)) {
            strategy = applicationContext.getBean(IpwhoisAnalyze.class);
        }else {
            strategy = applicationContext.getBean(Ip2regionAnalyze.class);
        }
        return strategy.analyze(ubrBo);
    }
}
